package com.sap.health.tracker.json;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
 
public class JSONFunctions {
 
    private JSONParser jsonParser;
 
    // Testing in localhost using wamp or xampp
    // use http://10.0.2.2/ to connect to your localhost if from emulator.  
    // private static String localhost = "http://10.0.2.2/CSSE3004/website/android_function/";
    // if you wish to debug on a device, both must be on the same IP (e.g, wifi)

    private static String localhost = "http://27.33.44.229/CSSE3004/website/android_function/";    
    private static String loginURL = localhost;
    private static String registerURL = localhost;
    private static String checkLoginURL = localhost;
    private static String sessionDestroyURL = localhost;

    private static String login_tag = "login";
    private static String register_tag = "register";
 
    // constructor
    public JSONFunctions(){
        jsonParser = new JSONParser();
    }
 
    /**
     * function check login request
     * @param userId
     * @param sessionToken
     * */
    public JSONObject checkLoginUser(String userId, String sessionToken){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "check_tag"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("sessionToken", sessionToken));
        JSONObject json = jsonParser.getJSONFromUrl(checkLoginURL, params);
        return json;
    }
    
    
    /**
     * function make Login Request
     * @param username
     * @param password
     * */
    public JSONObject loginUser(String username, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        return json;
    }
 
    /**
     * function make Register Request
     * @param username
     * @param email
     * @param password 
     * @param firstName 
     * @param lastName 
     * @param address1 
     * @param address2 
     * @param city 
     * @param state 
     * @param country 
     * @param postcode
     * @param contact 
     * @param dob 
     * @param gender 
     * @param allergies 
     * @param bloodtype 
     * @param benchmark
     * */
    public JSONObject registerUser(String username, String email, 
    		String password, String firstName, String lastName, String address1, 
    		String address2, String city, String state, String country, String postcode,
    		String contact, String dob, String gender, String allergies, String bloodtype, String benchmark){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("firstName", firstName));
        params.add(new BasicNameValuePair("lastName", lastName));
        params.add(new BasicNameValuePair("address1", address1));
        params.add(new BasicNameValuePair("address2", address2));
        params.add(new BasicNameValuePair("state", state));
        params.add(new BasicNameValuePair("country", country));
        params.add(new BasicNameValuePair("city", city));
        params.add(new BasicNameValuePair("postcode", postcode));

        params.add(new BasicNameValuePair("contact", contact));
        params.add(new BasicNameValuePair("dob", dob));
        params.add(new BasicNameValuePair("gender", gender));
        params.add(new BasicNameValuePair("allergies", allergies));
        params.add(new BasicNameValuePair("bloodtype", bloodtype));
        params.add(new BasicNameValuePair("benchmark", benchmark));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Doctor Profile Request
     * @param userId
     * @param session
     * */
    public JSONObject getDoctorProfile(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_profile"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make get Comparison Request
     * @param userId
     * @param session
     * @param datalogTypeId
     * @param order
     * */
    public JSONObject getComparison(String userId, String session, String datalogTypeId, String order){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_comparison"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("datalog_type_id", datalogTypeId));
        params.add(new BasicNameValuePair("order", order));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make get Graph Data Request
     * @param userId
     * @param session
     * @param graphType
     * @param compare
     * */
    public JSONObject getGraphData(String userId, String session, String graphType, String compare){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_graph_data"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("graphType", graphType));
        params.add(new BasicNameValuePair("compare", compare));

        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Add Symptom Request
     * @param userId
     * @param session
     * @param notes
     * @param symptoms
     * @param severity
     * */
    public JSONObject addSymptom(String userId, String session, String notes, String symptoms, String severity){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "add_symptom"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("notes", notes));
        params.add(new BasicNameValuePair("symptoms", symptoms));
        params.add(new BasicNameValuePair("severity", severity));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make get Patient Log Request
     * @param userId
     * @param session
     * @param patient_id
     * */
    public JSONObject getPatientLog(String userId, String session, String patient_id){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_patient_logs"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("patient_id", patient_id));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Add Food Request
     * @param userId
     * @param session
     * @param food
     * @param calories
     * @param notes
     * */
    public JSONObject addFood(String userId, String session, String food, String calories, String notes){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "add_food"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("food", food));
        params.add(new BasicNameValuePair("calories", calories));
        params.add(new BasicNameValuePair("notes", notes));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Add Log Request
     * @param userId
     * @param session
     * @param type
     * @param value
     * @param notes
     * */
    public JSONObject addLog(String userId, String session, String type, String value, String notes){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "add_log"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("type", type));
        params.add(new BasicNameValuePair("value", value));
        params.add(new BasicNameValuePair("notes", notes));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Notifications Request
     * @param userId
     * @param session
     * */
    public JSONObject getNotifications(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_notifications"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Hospitals Request
     * @param userid
     * @param session
     * */
    public JSONObject getHospitals(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_hospitals"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save Gender Request
     * @param userId
     * @param session
     * */
    public JSONObject saveGender(String userId, String session, 
    		String gender){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_gender"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("gender", gender));
        JSONObject json = null;
        // getting JSON Object
		json = jsonParser.getJSONFromUrl(registerURL, params);
		// return json
        return json;
    }
    
    /**
     * function make Push Notifications Request
     * @param userid
     * @param session
     * @param regId
     * */
    public JSONObject pushNotifications(String userId, String session, String regId){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "register_push_notifications"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("regId", regId));


        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save DOB Request
     * @param userId
     * @param session
     * @param year
     * */
    public JSONObject saveDOB(String userId, String session, String year, 
    		String month, String day){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_dob"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("year", year));
        params.add(new BasicNameValuePair("month", month));
        params.add(new BasicNameValuePair("day", day));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save Contact Request
     * @param userId
     * @param session
     * */
    public JSONObject saveContact(String userId, String session, 
    		String contact){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_contact"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("contact", contact));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save Address Request
     * @param userId
     * @param session
     * @param address
     * */
    public JSONObject saveAddress(String userId, String session, 
    		String address){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_address"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("address", address));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save Email Request
     * @param userId
     * @param session
     * @param email
     * */
    public JSONObject saveEmail(String userId, String session, 
    		String email){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_email"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("email", email));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Save Name Request
     * @param userid
     * @param session
     * @param firstname
     * @param lastname
     * */
    public JSONObject saveName(String userId, String session, 
    		String firstname, String lastname){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_name"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("firstname", firstname));
        params.add(new BasicNameValuePair("lastname", lastname));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Search Doctors Request
     * @param userId
     * @param session
     * @param search
     * */
    public JSONObject searchDoctorsList(String userId, String session, String search){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "search_doctors_list"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("search", search));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Doctors Request
     * @param userId
     * @param session
     * */
    public JSONObject getDoctorsList(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_doctors_list"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make get Conenctions Request
     * @param userId
     * @param session
     * */
    public JSONObject getConnectionList(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_connection_list"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Add Hospital Request
     * @param userId
     * @param session
     * @param hospital
     * */
    public JSONObject addHospital(String userId, String session, String hospital){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "add_hospital"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("hospital", hospital));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Remove Hospital Request
     * @param userId
     * @param session
     * @param hospital
     * */
    public JSONObject removeHospital(String userId, String session, String hospital){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "remove_hospital"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("hospital", hospital));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Food Request
     * @param userid
     * @param session
     * @param conectionId
     * */
    public JSONObject getFood(String userId, String session, String connectionId){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_food"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("connectionID", connectionId));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Symptoms Request
     * @param userId
     * @param session
     * @param connectionId
     * */
    public JSONObject getSymptoms(String userId, String session, String connectionId){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_symptoms"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("connectionID", connectionId));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Log Types Request
     * @param userId
     * @param session
     * */
    public JSONObject getLogTypes(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_logs_types"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Get Logs Request
     * @param userId
     * @param session
     * */
    public JSONObject getLogs(String userId, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_logs"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Remove Doctor Request
     * @param userId
     * @param session
     * @param doctor
     * */
    public JSONObject removeDoctor(String userId, String session, String doctor){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "remove_doctor"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("doctor", doctor));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Add Doctor Request
     * @param userId
     * @param session
     * @param doctor
     * @param note
     * */
    public JSONObject addDoctor(String userId, String session, String doctor, String note){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "add_doctor"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("doctor", doctor));
        params.add(new BasicNameValuePair("note", note));


 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Accept Patient Request
     * @param userId
     * @param session
     * @param patient
     * */
    public JSONObject acceptPatient(String userId, String session, String patient){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "accept_patient_request"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("patient", patient));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Remove Patient Request
     * @param userId
     * @param session
     * @param patient
     * */
    public JSONObject removePatientFrom(String userId, String session, String patient){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "remove_patient_request"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("patient", patient));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Delete Log Request
     * @param userid
     * @param sessionToken
     * @param logId
     * @param type
     * */
    public JSONObject deleteLog(String userId, String sessionToken, String logId, String type){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "delete_log"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("session", sessionToken));
        params.add(new BasicNameValuePair("logId", logId));
        params.add(new BasicNameValuePair("type", type));


        JSONObject json = jsonParser.getJSONFromUrl(sessionDestroyURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
 
    /**
     * function make Log out Request
     * @param userId
     * @param sessionToken
     * @param androidId
     * */
    public JSONObject logOutUser(String userId, String sessionToken, String androidId){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "delete_session_tag"));
        params.add(new BasicNameValuePair("userID", userId));
        params.add(new BasicNameValuePair("sessionToken", sessionToken));
        params.add(new BasicNameValuePair("androidID", androidId));

        JSONObject json = jsonParser.getJSONFromUrl(sessionDestroyURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
 
}